<?php
namespace Modules\Core\Services;

use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Request;
use ThrowException;

// use Modules\Core\Entities\FormId;
// use \EasyWeChat;
use Auth;
use Setting;
use Cache;

use Modules\Stats\Entities\UserStats;

class StatsService
{
    // 开奖后更新
    public function updateWithPackageSku($user, $activityType, $sku) 
    {
        $getMoneyPrice = $sku->money_price * $sku->total;
        $getScorePrice = $sku->score_price * $sku->total;
        $skuTotal = $sku->total;
        $useLuckyScore = 0;
        
        // 更新总的统计
        $day = NULL;
        $this->updateOrderMoney($user, $activityType, $day, [
            'pay_money_price' => 0,
            'pay_score_price' => 0,
            'get_money_price' => $getMoneyPrice,
            'get_score_price' => $getScorePrice,
            'sku_total' => $skuTotal,
            'use_lucky_score' => $useLuckyScore
        ]);

        // 更新当天的统计
        $day = date('Y-m-d');
        $this->updateOrderMoney($user, $activityType, $day, [
            'pay_money_price' => 0,
            'pay_score_price' => 0,
            'get_money_price' => $getMoneyPrice,
            'get_score_price' => $getScorePrice,
            'sku_total' => $skuTotal,
            'use_lucky_score' => $useLuckyScore
        ]);
    }

    // 开奖后更新
    public function updateWithPackageAndOrder($user, $activityType, $package, $order) 
    {
        $getMoneyPrice = 0;
        $getScorePrice = 0;
        $skuTotal = 0;
        $useLuckyScore = 0;
        
        foreach ($package->skus as $sku) {
            $getMoneyPrice += $sku->money_price * $sku->total;
            $getScorePrice += $sku->score_price * $sku->total;
            $skuTotal += $sku->total;
            $useLuckyScore += $sku->use_lucky_score * $sku->total;
        }

        // 更新总的统计
        $day = NULL;
        $this->updateOrderMoney($user, $activityType, $day, [
            'pay_money_price' => $order->order_money_price,
            'pay_score_price' => $order->order_score_price,
            'get_money_price' => $getMoneyPrice,
            'get_score_price' => $getScorePrice,
            'sku_total' => $skuTotal,
            'use_lucky_score' => $useLuckyScore
        ]);

        // 更新当天的统计
        $day = date('Y-m-d');
        $this->updateOrderMoney($user, $activityType, $day, [
            'pay_money_price' => $order->order_money_price,
            'pay_score_price' => $order->order_score_price,
            'get_money_price' => $getMoneyPrice,
            'get_score_price' => $getScorePrice,
            'sku_total' => $skuTotal,
            'use_lucky_score' => $useLuckyScore
        ]);
    }

    // 更新用户的统计
    public function updateOrderMoney($user, $activityType, $day, $options = [])
    {
        $stats = UserStats::where('user_id', $user->id)->where('day', $day)->first();

        // 如果没有统计实例，则创建
        if (!$stats) {
            $stats = UserStats::create([
                'user_id' => $user->id,
                'day' => $day
            ]);
        }

        $payMoneyKey = $activityType . '_pay_money_price';
        $payScoreKey = $activityType . '_pay_score_price';

        $getMoneyKey = $activityType . '_get_money_price';
        $getScoreKey = $activityType . '_get_score_price';

        $skuTotalKey = $activityType . '_sku_total';

        // $allPayMoney = $item->all_pay_money_price + $payMon

        $stats->update([
            $payMoneyKey =>  $stats->$payMoneyKey + (@$options['pay_money_price'] ?: 0),
            $payScoreKey =>  $stats->$payScoreKey + (@$options['pay_score_price'] ?: 0),
            $getMoneyKey =>  $stats->$getMoneyKey + (@$options['get_money_price'] ?: 0),
            $getScoreKey =>  $stats->$getScoreKey + (@$options['get_score_price'] ?: 0),
            $skuTotalKey => $stats->$skuTotalKey + (@$options['sku_total'] ?: 0)
        ]);

        // 更新总统计数量
        $stats->updateAllStats();

        // 福袋增减欧气
        if (($activityType === 'fudai') && !$day) {
            $newLuckyScore = $user->lucky_score -= (@$options['get_money_price'] - @$options['pay_money_price']) / 100;

            $newLuckyScore -= @$options['use_lucky_score'] ?: 0;

            $user->update([
                'lucky_score' => $newLuckyScore
            ]);
        }

    }
}
